package cn.itcast.service.impl;

import cn.itcast.clients.OrderClients;
import cn.itcast.clients.RoomClients;
import cn.itcast.clients.UserClients;
import cn.itcast.entity.Cast;
import cn.itcast.mapper.CastMapper;

import cn.itcast.entity.Order;
import cn.itcast.entity.PageBean;
import cn.itcast.service.CastService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.stereotype.Service;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.List;

@Service
@EnableFeignClients
public class CastServiceImpl implements CastService {
    @Autowired
    private CastMapper castMapper;
    @Autowired
    private OrderClients orderClients;
    @Autowired
    private RoomClients roomClients;
    @Autowired
    private UserClients userClients;
    @Override
    public void add(Order order) {
        Cast cast = new Cast();
        LocalDateTime start= order.getCheckInDate();//
        LocalDateTime end=order.getCheckOutDate();
        Duration d=Duration.between(start,end);
        //总金额
        Result<Room> room = roomClients.findById(order.getRoomId());
        long l = d.toHours();
        long l1 = l % 12;
        if (l1==0){
            long l2 = l * 10;
            cast.setElectricity(l2);
            cast.setWash(l2);
            cast.setMoney(room.getData().getPrice()-l2-l2);
        }else {
            long l2 = l / 12;
            long l3 = (l2 + 1) * 10;
            cast.setElectricity(l3);//水电费
            cast.setWash(l3);//保洁费
            cast.setMoney(room.getData().getPrice()-l3-l3);
        }
        cast.setCreateTime(order.getCheckInDate());//创建时间
        cast.setLeaveTime(order.getCheckOutDate());//离店时间
        //盈利
        if (cast.getMoney()==0){
            cast.setStatus(0);
        } else if (cast.getMoney()<0) {
            cast.setStatus(2);

        }else {cast.setStatus(1);}
        cast.setRoomId(order.getRoomId());
        cast.setUserId(order.getUserId());
        cast.setOrderId(order.getId());
        Result<User> id = userClients.findById(cast.getId());
        cast.setUserName(id.getData().getName());
       /* cast.setElectricity(12l);
        cast.setWash(12l);
        cast.setMoney(200d);
        cast.setStatus(2);
        cast.setUserName("卡拉米");
        cast.setCreateTime(order.getCheckInDate());
        cast.setLeaveTime(order.getCheckOutDate());*/
        castMapper.add(cast);

    }

/*    @Override
    public Cast selectById(Integer id) {
        Cast Cast=castMapper.selectById1(id);
        return Cast;
    }

    @Override
    public void update(Cast Cast) {
        castMapper.update(Cast);

    }*/
//带条件分页查询
    @Override
    public PageBean page(Integer page, Integer pageSize, String userName, String roomId) {
        PageHelper.startPage(page,pageSize);
        List<Cast> castList = castMapper.list(userName, roomId);
        Page<Cast> p =(Page<Cast>) castList;
        PageBean pageBean = new PageBean(p.getTotal(), p.getResult());
        return pageBean;
    }

    @Override
    public Double getMoney(Long userId) {
        Double money=castMapper.getMoney(userId);
        return money;
    }


}
